package practice.system.entry;

import practice.system.domain.Student;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashSet;
import java.util.Scanner;

public class Test {
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) throws Exception {

        while (true) {
            System.out.println("请输入您的选择: 1.添加学生信息  2.查询学生信息 0.关闭系统");
            String choice = sc.next();
            switch (choice) {
                case "1": {
                    addStudent();
                    break;
                }
                case "2": {
                    findStudent();
                    break;
                }
                case "0": {
                    System.exit(0);
                }
                default: {
                    System.out.println("输入有误，重新输入");
                    break;
                }
            }
        }
    }

    private static void findStudent() throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("day15/student.txt"));
        HashSet<String> set = new HashSet<>();
        String line;
        while ((line = br.readLine()) != null) {
            set.add(line);
        }
        set.forEach((s) -> {
            System.out.println(s);
        });
        //System.out.println(set);
        br.close();

    }

    private static void addStudent() throws Exception {
        System.out.println("请输入学生的姓名");
        String name = sc.next();
        System.out.println("请输入学生的年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生的地址");
        String address = sc.next();
        //Student student = new Student(name, age, address);
        BufferedWriter bw = new BufferedWriter(new FileWriter("day15/student.txt", true));
        String s = name + "," + age + "," + address;
        bw.write(s);
        bw.newLine();
        bw.close();
    }
}
